package com.android.activity.fragment;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.android.R;
import com.android.activity.adapter.GoodsTypeFSAdapter;
import com.android.bean.BaseRecords;
import com.android.bean.Good;
import com.android.bean.GoodRecords;
import com.android.bean.GoodsType;
import com.android.bean.ResponseData;
import com.android.bean.User;
import com.android.bean.queryBean.UserGoodsRecords;
import com.android.model.goods.GoodsListener;
import com.android.model.goods.GoodsModelimpl;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 *com.android.activity.fragment.HomePageFragment
 * create an instance of this fragment.
 */
public class HomePageFragment extends Fragment implements View.OnClickListener, GoodsListener {
    private Context context = null;
    private Button button;
    private GoodsModelimpl goodsModelimpl;
    private List<GoodsType> goodsTypeList;
    private List<Good> goodList;
    private View rootView;
    private GoodsTypeFSAdapter goodsTypeFSAdapter;
    private User user;

    private ViewPager2 viewPager;




    public HomePageFragment() {
        // Required empty public constructor

    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);

        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        context = getActivity();

        rootView = inflater.inflate(R.layout.fragment_home_page,
                container, false);
        initGoodsTypeData();
        return rootView;

    }
    private void initGoodsTypeData() {
        if (user != null){
            goodsModelimpl = new GoodsModelimpl(user);
        }else {
            goodsModelimpl = new GoodsModelimpl();

        }
//        Map<String, Object> map = new HashMap<>();
        goodsModelimpl.getAllGoodsType(this);
        Log.e("1",String.valueOf(user.getId()));
        //发送获取用户交易记录
//        goodsModelimpl.getUserRecords(user.getId(),map,this);


    }


    //获取商品类型成功回调
    @Override
    public void onGetAllGoodsType(ResponseData<List<GoodsType>> responseData) {
        this.goodsTypeList = responseData.getData();
        goodsModelimpl.getAllGood(this);

    }

    //获取商品成功回调
    @Override
    public void onGetAllGoodSuccess(ResponseData<GoodRecords> responseData) {
        if(responseData.getData().getTotal()>0){
            this.goodList = responseData.getData().getRecords();
            initRecyclerview();
//            Log.e("goodList",goodList.get(1).getUsername());
        }
    }

    //获取用户交易记录
    @Override
    public void onGetUserRecordsSuccess(ResponseData<BaseRecords<UserGoodsRecords>> responseData) {
        //通过EventBus传输用户的交易记录
//        EventBus.getDefault().postSticky(responseData.getData());
    }

    private void initRecyclerview() {
        viewPager = rootView.findViewById(R.id.view_pager);
        ArrayList<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < goodsTypeList.size(); i++) {
            fragments.add(GoodShowFragment.newInstance(goodsTypeList.get(i).getId(), goodList));
        }

        goodsTypeFSAdapter = new GoodsTypeFSAdapter(getActivity().getSupportFragmentManager(), getLifecycle(),fragments);
        viewPager.setAdapter(goodsTypeFSAdapter);
        //把TabLayout（选项卡布局）和ViewPager2绑定在一起。特别说明一下，下面这行代码是官方给的，特别好使。
        TabLayout tablayout = rootView.findViewById(R.id.tabLayout);
        new TabLayoutMediator(tablayout, viewPager,
                (tab, position) -> tab.setText(goodsTypeList.get(position).getType())).attach();

    }



    @Override
    public void onClick(View v) {

    }

    @Subscribe(threadMode = ThreadMode.POSTING,sticky = true)
    public void getLoginUser(ResponseData<User> responseData){
        this.user = responseData.getData();
    }
}